package com.jingdianjichi.subject.domain.handler.subject;

import com.jingdianjichi.subject.common.enums.SubjectInfoTypeEnum;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Description: 题目类型工厂
 * Copyright: Copyright (c) 2024
 * Create Time: 2024-07-15 15:15
 *
 * @author wanglianyun
 */
@Component
public class SubjectTypeHandlerFactory implements InitializingBean {

    // 利用spring注入将SubjectTypeHandler的实现类放到list中
    @Resource
    private List<SubjectTypeHandler> subjectTypeHandlerList;

    private Map<SubjectInfoTypeEnum, SubjectTypeHandler> handlerMap = new HashMap<>();

    public SubjectTypeHandler getHandler(int subjectType) {
        SubjectInfoTypeEnum byCode = SubjectInfoTypeEnum.getByCode(subjectType);

        return handlerMap.get(byCode);

    }

    @Override
    public void afterPropertiesSet() throws Exception {
        for (SubjectTypeHandler subjectTypeHandler : subjectTypeHandlerList) {
            handlerMap.put(subjectTypeHandler.getHandlerType(), subjectTypeHandler);
        }
    }
}
